package controllers;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.joda.time.DateTime;

import play.libs.Codec;
import services.CaseService;
import services.DictService;
import services.DrCaseSortService;
import services.DrCourtService;
import vos.ResultVo;
import vos.ReturnCode;
import models.DrCase;
import models.DrCaseConfig;
import models.DrCaseTemp;
import models.DrCasesort;
import models.DrCourt;
import models.DrMicrophone;
import models.SystemOperator;

/**
 * 案件控制类
 * @author wangshaoming
 *
 */
public class DrCaseController extends BaseController {
	private static final SimpleDateFormat dateFormater = new SimpleDateFormat("yyyy-MM-dd");

	
	/**
	 * 保存案件
	 * @param cases
	 * @param configs 案件基本配置项
	 * @param micros 麦克风列表
	 */
	public static void saveCase(DrCaseTemp cases,DrCaseConfig[] configs,DrMicrophone[] micros){
		if(StringUtils.isEmpty(cases.getCourtid())){
			renderRespondJson(ReturnCode.ErrorCode, "请选择法庭");
		}
		if(StringUtils.isEmpty(cases.getSortid())){
			renderRespondJson(ReturnCode.ErrorCode, "请选择案件种类");
		}
		if(StringUtils.isEmpty(cases.getCasecode().trim())){
			renderRespondJson(ReturnCode.ErrorCode, "案件编号不可为空");
		}
		if(cases.getCasecode().trim().length()>50){
			renderRespondJson(ReturnCode.ErrorCode, "案件编号超出长度限制");
		}
		DrCase oldCases=CaseService.getByCaseCode(cases.getCasecode().trim());
		if(oldCases!=null){
			renderRespondJson(ReturnCode.ErrorCode, "该案件已存在");
		}
		if(StringUtils.isEmpty(cases.getCasename().trim())){
			renderRespondJson(ReturnCode.ErrorCode, "案件名称不可为空");
		}
		if(cases.getCasename().trim().length()>50){
			renderRespondJson(ReturnCode.ErrorCode, "案件名称超出长度限制");
		}
		if(micros==null||micros.length<=0){
			renderRespondJson(ReturnCode.ErrorCode, "无麦克风输入");
		}
		DrCourt court= DrCourtService.getById(cases.getCourtid());
		if(court==null){
			renderRespondJson(ReturnCode.ErrorCode, "选择的法庭不存在");
		}
		DrCasesort sort=DrCaseSortService.getById(cases.getSortid());
		if(sort==null){
			renderRespondJson(ReturnCode.ErrorCode, "选择的案件种类不存在");
		}
		if(cases.getOpendate()==null){
			renderRespondJson(ReturnCode.ErrorCode, "请选择开庭时间");
		}
		String today=dateFormater.format(new Date());
		Date date=DateTime.parse(today).toDate();
		if(cases.getOpendate().getTime()<date.getTime()){
			renderRespondJson(ReturnCode.ErrorCode, "开庭时间不可小于当前时间");
		}
		if(StringUtils.isEmpty(cases.getIspublic())){
			renderRespondJson(ReturnCode.ErrorCode, "请选择是否公开");
		}
		if(DictService.getById(cases.getIspublic())==null){
			renderRespondJson(ReturnCode.ErrorCode, "请选择是否公开");
		}
		//验证法庭是否正在使用中
		if(new BigDecimal(2).equals(court.getStatus())){
			renderRespondJson(ReturnCode.ErrorCode, "法庭已停用");
		}
		if(new BigDecimal(3).equals(court.getStatus())){
			renderRespondJson(ReturnCode.ErrorCode, "法庭正在使用中");
		}
		SystemOperator operator=getLoginUser();
		String caseid=Codec.UUID();
		cases.setStatus(new BigDecimal(0));
		cases.setCaseid(caseid);
		cases.setCreatename(operator.getOperatorid());
		String ip=request.remoteAddress;
		boolean issave=CaseService.saveCase(cases,configs,micros,ip,getLoginUser(),court,sort.getSortname());
		if(issave){
			Map<String,String> map=new HashMap<String,String>();
			map.put("caseid", caseid);
			map.put("courtid", cases.getCourtid());
			ResultVo vo=new ResultVo(ReturnCode.SuccessCode,"保存成功",map);
			renderJSON(vo);
		}
		renderRespondJson(ReturnCode.ErrorCode, "保存失败");
	}
}
